import { distributeSub, referralGrades } from '../../utils/requestApi.js';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.data.imgurl,
    activeItem: 1,
    timeArr: ['全部','近7天', '近30天','近半年'],
    seTime: null,  //筛选时间
    joinTimeIcon: 0,
    totalPriceIcon: 0,
    empolyIcon: 0,
    list: [],
    listData: null,
    page: 1, // 页码
    obj: {}, // 排序数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initData({ gradeId: this.data.activeItem });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  selectNav:function(e){
    let { id } = e.target.dataset;
    this.setData({ activeItem: id })
    this.initData({ gradeId: id })
  },

  //筛选时间
  selectTime:function(e){
    this.setData({ seTime: this.data.timeArr[e.detail.value] })
  },
  
  //搜索
  search: function (e) {
    let { value } = e.detail;
    this.initData({ search: value })
  },
  
  //排序
  attrSort:function(e){
    let { id } = e.currentTarget.dataset;
    let { joinTimeIcon, totalPriceIcon, empolyIcon } = this.data, obj = {};
    if (id == 1){
      totalPriceIcon = 0;
      empolyIcon = 0;
      joinTimeIcon = joinTimeIcon == 0 ? 1 : (joinTimeIcon == 1 ? 2 : 0)
      if (joinTimeIcon != 0){
        obj.sortOrder = joinTimeIcon == 1 ? false : true
        obj.sortBy = 'CreateDate'
      }
    }
    if (id == 2) {
      joinTimeIcon = 0;
      empolyIcon = 0;
      totalPriceIcon = totalPriceIcon == 0 ? 1 : (totalPriceIcon == 1 ? 2 : 0)
      if (totalPriceIcon != 0) {
        obj.sortOrder = totalPriceIcon == 1 ? false : true
        obj.sortBy = 'SubSumOrderTotal'
      }
    }
    if (id == 3) {
      joinTimeIcon = 0;
      totalPriceIcon = 0;
      empolyIcon = empolyIcon == 0 ? 1 : (empolyIcon == 1 ? 2 : 0) 
      if (empolyIcon != 0) {
        obj.sortOrder = empolyIcon == 1 ? false : true
        obj.sortBy = 'SubMemberAllSplittin'
      }
    }
    this.setData({ joinTimeIcon, totalPriceIcon, empolyIcon, obj})
    this.initData(obj)
  },
  
  //初始化页面数据
  initData: function (obj){
    wx.showLoading({ title: '加载中...' })
    let { page, listData, list } = this.data;
    if (!obj.onReach) list = [];
    distributeSub({
      pageSize: 10,
      pageIndex: page,
      SortBy: obj.sortBy || '',
      SortOrder: obj.sortOrder || '',
      StartTime: obj.startTime || '',
      search: obj.search || '',
      GradeId: obj.gradeId || ''
    }).then(res =>{
      wx.hideLoading();
      if (res.data.Status == 'Success' && res.data.MySubUsersList.length != 0){
        list = [...list, ...res.data.MySubUsersList];
      }
      this.setData({ listData: res.data, list })
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let { obj } = this.data;
    obj.onReach = true; // 用于判断是否在做上拉操作
    this.setData({ obj })
    this.initData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})